import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
    state: {
        count: 1,
        todos: [
            { id: 1, name: 'lucy', done: true, sex: 0 },
            { id: 2, name: 'tom', done: false, sex: 1 },
            { id: 3, name: 'tom', done: false, sex: 1 }
        ],
        sex: 1
    },
    getters: {
        getNan(state) {
            return state.todos.filter(todo => todo.sex == 1);
        },
        getSex(state) {
            if (state.sex == 1) {
                return '男';
            } else {
                return '女';
            }
        },
        getTodoSize(state, getters) {
            return getters.getNan.length;
        },
        getTodoById: (state) => (id) => {
            return state.todos.find(todo => todo.id === id)
        }
    },
    mutations: {
        add1(state, n) {
            //不能写异步操作
            // setTimeout(function() {
            //     state.count = state.count + n;
            // }, 2000)
            state.count = state.count + n;

        }
    },
    actions: {
        asyncAdd(context, n) {
            context.commit('add1', n)
        }
    },
    modules: {}
})